package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.dto.PageDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.UserService;
import com.example.mybatisplus.model.domain.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *  前端控制器
 *
 *
 * @author blue
 * @since 2022-01-05
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private UserService userService;

    //登录模块
    @RequestMapping("/login")
    public  JsonResponse login(User user) {
        //先查这个用户  Controller层 到 Service层 调用Service层的方法

        //login分为两种情况--》1、有值  2、null
        User login = userService.login(user); //在Service层实现login方法(alt + enter)
        if (login!=null){
            //存进session
            SessionUtils.saveLoginUser(login);
        }
        return JsonResponse.success(login);
    }


    //注册模块
    @RequestMapping("/regist")
    public JsonResponse regist(User user) {
        //逻辑必须写在Service里面
        JsonResponse jsonResponse = userService.regist(user);

        return jsonResponse;
    }

    //列表分页
    @GetMapping("/list")
    public JsonResponse list(PageDTO page,User user) {
        //创建一个分页器
        Page<User> page1 = (Page<User>) userService.listByPage(page,user);
        return JsonResponse.success(page1);
    }

    //用户列表的删除  （根据用户id）
    @GetMapping("/deleteById")
    public JsonResponse deleteById(Long id) {
        userService.removeById(id);
        return JsonResponse.success(true, "删除成功");
    }
    //用户列表的编辑
    @PostMapping("/updateById")
    public JsonResponse updateById(@RequestBody User user) {
        userService.updateById(user);
        return JsonResponse.success(true, "修改成功");
    }
    //根据id查出用户信息
    @RequestMapping("/userById")
    public JsonResponse userById(Long id) {
        System.out.println(id);
        User user = userService.getById(id);
        System.out.println(user);
        return JsonResponse.success(user);
    }



















































//    /**
//    * 描述：根据Id 查询
//    *
//    */
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
//        User  user =  userService.getById(id);
//        return JsonResponse.success(user);
//    }
//
//    /**
//    * 描述：根据Id删除
//    *
//    */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    @ResponseBody
//    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
//        userService.removeById(id);
//        return JsonResponse.success(null);
//    }
//
//
//    /**
//    * 描述：根据Id 更新
//    *
//    */
//    @RequestMapping(value = "", method = RequestMethod.PUT)
//    @ResponseBody
//    public JsonResponse updateUser(User  user) throws Exception {
//        userService.updateById(user);
//        return JsonResponse.success(null);
//    }
//
//
//    /**
//    * 描述:创建User
//    *
//    */
//    @RequestMapping(value = "", method = RequestMethod.POST)
//    @ResponseBody
//    public JsonResponse create(User  user) throws Exception {
//        userService.save(user);
//        return JsonResponse.success(null);
//    }
}

